package com.hbzjjzx.athlete.athlete.controller;


import com.hbzjjzx.athlete.athlete.apiInterface.CoachService;
import com.hbzjjzx.athlete.athlete.domain.AtsEmpAthlete;
import com.hbzjjzx.athlete.athlete.domain.Coach;
import com.hbzjjzx.athlete.athlete.service.AtsEmpAthleteService;
import com.hbzjjzx.athlete.athlete.utils.Message;
import com.hbzjjzx.athlete.athlete.utils.Page;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @program: hbats
 * @description: 运动员信息控制器
 * @author: 张超
 * @create: 2019-10-29 18:39
 */

@Api(description = "湖北ats运动员管理接口文档")
@RestController
public class AtsEmpAthleteController {

    Logger logger = LoggerFactory.getLogger(getClass());

    /*
    是Spring用于同步client端的核心类，简化了与http服务的通信，并满足RestFul原则，程序代码可以给它提供URL，并提取结果。
     */
    @Resource
    RestTemplate restTemplate ;

    @Resource
    DiscoveryClient discoveryClient;


    @Resource
    CoachService coachService;

    /**
     * 运动员信息接口
     */
    @Resource
    private AtsEmpAthleteService atsEmpAthleteService;

    @ApiOperation(value = "增加或者修改运动员" ,  notes="如果增加成功返回1,失败返回0")
    @PostMapping("athlete")
    public Message insertOrUpdate(@RequestBody  AtsEmpAthlete atsEmpAthlete){

        int rows = atsEmpAthleteService.saveOrUpdate(atsEmpAthlete);

        Message message = new Message();
        message.setNum(rows);
        message.setMsg("编辑成功");
        return message;
    }


    /**
     * 根据id查找
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询运动员" ,  notes="如果查询成功返回运动员json,失败则返回空")
    @GetMapping("athlete/{id}")
    //@HystrixCommand(fallbackMethod = "errorBack")
    public AtsEmpAthlete findById(@PathVariable("id") Integer id){
        logger.info("查询运动员开始"+id);

        AtsEmpAthlete ats= atsEmpAthleteService.findById(id);

        //String coachServiceId = "coachService";
//        //根据一个serviceId可以得到一个微服务的组合
//        List<ServiceInstance> list = discoveryClient.getInstances(coachServiceId);
//
//        ServiceInstance instance = list.get(0);//取第一个微服务实例
//
//        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/coach/1";

       // String url = "http://coachService/coach/1";

        Coach coach =coachService.findById(ats.getCoachId()); //restTemplate.getForObject(url, Coach.class);
        ats.setCoachName(coach.getCoachName());
        logger.info("查询运动员结束"+id);
        return ats;
    }

    /**
     * 熔断机制的回调方法
     * @return
     */
    public AtsEmpAthlete errorBack(Integer id){
        AtsEmpAthlete ats = new AtsEmpAthlete();
        ats.setCoachName("报错,无法找到服务");
        return ats ;
    }


    /**
     * 根据id查找
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询运动员2" ,  notes="如果查询成功返回运动员json,失败则返回空")
    @GetMapping("athlete2/{id}")
    public Callable<AtsEmpAthlete> findById2(@PathVariable("id") Integer id){
        logger.info("查询运动员开始222222"+id);
        Callable<AtsEmpAthlete> call = new Callable<AtsEmpAthlete>() {
            @Override
            public AtsEmpAthlete call() throws Exception {

                Thread.sleep(2000);

                AtsEmpAthlete ats = atsEmpAthleteService.findById(id);

                return ats;
            }
        };
        logger.info("查询运动员结束222222"+id);
        return call ;
    }


    /**
     * 查询所有  返回map
     * @param atsEmpAthlete
     * @param page
     * @return
     */
    @ApiOperation(value = "查询所有的运动员" ,  notes="分页显示,返回的结果是json")
    @PostMapping("athletes")
    public Map selectAll(@RequestBody AtsEmpAthlete atsEmpAthlete,Page page){
        Map map = new HashMap();

        List<AtsEmpAthlete> list = atsEmpAthleteService.selectAll(atsEmpAthlete,page);

        int count = atsEmpAthleteService.selectCount(atsEmpAthlete);
        //int count = 10;
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);
        return map;
    }

    /**
     * 按id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除运动员" ,  notes="如果增加成功返回1,失败返回0")

    @DeleteMapping("athlete/{id}")
    public Message delete(@PathVariable("id")  Integer id){
        Message message = new Message();
        if(id==null){
            message.setNum(0);
            message.setMsg("删除失败");
            return message;
        }else{
            AtsEmpAthlete atsEmpAthlete = new AtsEmpAthlete();
            atsEmpAthlete.setAthleteId(id);
            atsEmpAthlete.setIsva(2);
            atsEmpAthleteService.saveOrUpdate(atsEmpAthlete);

            message.setNum(1);
            message.setMsg("删除成功");
            return message;
        }


    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("deleteAll")
    @ResponseBody
    public Message deleteAll(String ids){
        Message message = new Message();
        if(atsEmpAthleteService.deleteAll(ids)==0){
            message.setNum(0);
            message.setMsg("删除失败");
        }else{
            message.setNum(1);
            message.setMsg("删除成功");
        }
        return message;
    }

    /**
     * 查询所有  返回list
     * @param atsEmpAthlete
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public List<AtsEmpAthlete> findAll(AtsEmpAthlete atsEmpAthlete){
        return atsEmpAthleteService.findAll(atsEmpAthlete);
    }

    /**
     * 根据训练项目ID查询运动员  返回list
     * @param atsEmpAthlete
     * @return
     */
    @RequestMapping("selectBysportId")
    @ResponseBody
    public List<AtsEmpAthlete> selectBysportId(AtsEmpAthlete atsEmpAthlete){
        return atsEmpAthleteService.selectBysportId(atsEmpAthlete);
    }


    /**
     * 根据运动教练员查询运动员  返回list
     * @param coachId
     * @return
     */
    @RequestMapping("selectByCoachId")
    @ResponseBody
    public List<AtsEmpAthlete> selectByCoachId(Integer coachId){
        return atsEmpAthleteService.selectByCoachId(coachId);
    }


    /**
     * 根据科研教练员查询运动员  返回list
     * @param oldCoach
     * @return
     */
    @RequestMapping("selectByOldCoach")
    @ResponseBody
    public List<AtsEmpAthlete> selectByOldCoach(Integer oldCoach){
        return atsEmpAthleteService.selectByOldCoach(oldCoach);
    }
}